package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;

import model.College;
import model.Teacher;
import types.ConsoleType;
import view.PanelHolder;
import view.PanelTeacherDetails;
import view.PanelTeacherList;
import dataLayer.DAOTeacher;
import dataLayer.DBconnect;

public class EditTeacher extends DBconnect implements ActionListener {

	private PanelTeacherDetails PTDform;

	private String Teacher_codeOld;
	private String Teacher_code;
	private String Teacher_name;
	private JButton opslaan;
	private JButton annuleren;
	private JTextField JTname;
	private JTextField JTcode;
	private JButton Wijzig;
	private JButton Verwijder;
	private JLabel JLname;
	private JLabel JLcode;
	private JLabel JLname2;
	private JLabel JLcode2;

	public EditTeacher(PanelTeacherDetails PTDform) {
		this.setPTDform(PTDform);
	

	}

	// Check on fields for EditTeacher and message in console
	@Override
	public void actionPerformed(ActionEvent e) {
		openConn();
		Teacher t = new Teacher(Teacher_codeOld, Teacher_code, Teacher_name);
		t = PTDform.getTeacher();
		Teacher t1 = new Teacher(t.getTeacher_code(), Teacher_code,
				Teacher_name);
		
		if (e.getActionCommand().equals("Opslaan")) {
			String message = null;
			ConsoleType type = null;

			try {
				Teacher_code = PTDform.getJTCode().getText();
				Teacher_name = PTDform.getJTName().getText();
			
				t1.setTeacher_code(Teacher_code.toUpperCase());
				t1.setTeacher_name(Teacher_name);
				if (Teacher_code.equals("") && Teacher_name.equals("")) { // alles
																			// leeg
					message = "Er is nog niets ingevuld.";
					type = ConsoleType.EMPTY;
					Console.getInstance().setConsole(message, type);
				} else if (Teacher_code.equals("")) { // alleen code leeg
					message = "Er is nog geen code ingevuld.";
					type = ConsoleType.EMPTY;
				} else if (Teacher_name.equals("")) { // alleen naam leeg
					message = "Er is nog geen naam ingevuld.";
					type = ConsoleType.EMPTY;
				} else if (Teacher_code.length() > 10) { // code te lang
					message = "Code mag maximaal 10 tekens bevatten.";
					type = ConsoleType.ERROR;
				} else if (Teacher_name.length() > 50) { // naam te lang
					message = "Naam mag maximaal 50 tekens bevatten.";
					type = ConsoleType.ERROR;
				} else if (!(t1.getTeacher_code().equals(t1.getTeacher_oldCode())) && !College.getInstance().addTeacher(t1)) { // Code bestaat al
					message = "Er bestaat al een docent met deze code.";
					type = ConsoleType.ERROR;
				} else {
					// Als de code gelijk blijft
					if (t1.getTeacher_code().equals(t1.getTeacher_oldCode())) { 
						College.getInstance().getAllactiveTeachers().remove(t);
					}

					message = Teacher_name + " is aangepast.";
					type = ConsoleType.SUCCESS;

					College.getInstance().getAllactiveTeachers().remove(t);
					DAOTeacher.getInstance().updateTeacher(t1);
					College.getInstance().getAllactiveTeachers().add(t1);

					annuleren = PTDform.getAnnuleren();
					opslaan = PTDform.getOpslaan();
					Wijzig = PTDform.getWijzig();
					Verwijder = PTDform.getDelete();
					JTname = PTDform.getJTName();
					JTcode = PTDform.getJTCode();
					JLcode = PTDform.getJLcode();
					JLname = PTDform.getJLname();
					JLcode2 = PTDform.getJLcode2();
					JLname2 = PTDform.getJLname2();

					opslaan.setVisible(false);
					JTname.setVisible(false);
					JTcode.setVisible(false);
					annuleren.setVisible(false);

					JLname.setVisible(true);
					JLcode.setVisible(true);
					JLcode2.setVisible(true);
					JLname2.setVisible(true);
					Wijzig.setVisible(true);
					Verwijder.setVisible(true);

					// Reload all Teachers repaint PanelTeacherList
					College.getInstance().setAllactiveTeachers();
					PanelTeacherList.getInstance().repaint();

					PanelHolder ph = PanelHolder.getInstance();
					ph.remove(PTDform);
					ph.add(PanelTeacherList.getInstance());
					ph.repaint();
					ph.revalidate();
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
			Console.getInstance().setConsole(message, type);
		}
	}



	public PanelTeacherDetails getPTDform() {
		return PTDform;
	}

	public void setPTDform(PanelTeacherDetails pTDform) {
		PTDform = pTDform;
	}

	public String getTeacher_codeOld() {
		return Teacher_codeOld;
	}

	public void setTeacher_codeOld(String teacher_codeOld) {
		Teacher_codeOld = teacher_codeOld;
	}

}